package com.bjsxt.web.servlet;

import com.bjsxt.pojo.Users;
import com.bjsxt.service.UserManagerService;
import com.bjsxt.service.impl.UserManagerServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/*用户管理服务层*/
@WebServlet("/userManager.do")
public class UserManagerServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String flag = req.getParameter("flag");
        if("addUser".equals(flag)){
            this.addUser(req,resp);
        }else if("findUser".equals(flag)){
            this.findUser(req,resp);
        }else if("preUpdate".equals(flag)){
            this.preUpdate(req,resp);
        }else if("modifyUser".equals(flag)){
            this.modifyuser(req,resp);
        }else {
            this.dropUser(req,resp);
        }

    }

    //处理添加用户请求  首先要添加用户的数据  然后将得到的user放进业务层 实例化业务层 跳转到新页面（重定向）
    private void addUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        //获得从前端得到的用户
        Users users = this.createUser(req);
        try{
            UserManagerService userManagerService = new UserManagerServiceImpl();
            userManagerService.addUser(users);
            //防止表单重复提 用重定向
            resp.sendRedirect("ok.jsp");
        }catch (Exception e){
            e.printStackTrace();
            resp.sendRedirect("error.jsp");
        }
    }
    //获取添加用户的数据  只要HttpServletRequest 参数  在表单中获得数据
    private Users createUser(HttpServletRequest req){
        String username = req.getParameter("username");
        String userpwd = req.getParameter("userpwd");
        String usersex = req.getParameter("usersex");
        String phonenumber = req.getParameter("phonenumber");
        String qqnumber = req.getParameter("qqnumber");
        Users users = new Users();
        users.setUsername(username);
        users.setUserpwd(userpwd);
        users.setUsersex(usersex);
        users.setPhonenumber(phonenumber);
        users.setQqnumber(qqnumber);
        return users;
    }

    //查询请求  获得表单中的查询请求的条件  在createUser中获得数据
    private void findUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        Users users = this.createUser(req);
        try{
            //实例化 UserManagerService 调用里面的方法 就会运行持久层的代码
            UserManagerService userManagerService = new UserManagerServiceImpl();
            //将查询到的结果 放进list中 然后再请求转发发送到页面
            List<Users> list = userManagerService.finduser(users);
            req.setAttribute("list",list);

            req.getRequestDispatcher("usermanager/viewUser.jsp").forward(req,resp);
        }catch (Exception e){
            e.printStackTrace();
            resp.sendRedirect("error.jsp");
        }
    }


    //预更新查询  先取到用户提交的id 再实例化业务层方法 把id传过去  最后把要更新的用户用req.setAttribute传到servletContext 再用请求转发跳到修改的页面
    private void preUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String userid = req.getParameter("userid");
        try{
            UserManagerService userManagerService = new UserManagerServiceImpl();
            Users users = userManagerService.findUserById(Integer.parseInt(userid));
            req.setAttribute("users",users);
            req.getRequestDispatcher("usermanager/updateUser.jsp").forward(req,resp);
        }catch (Exception e){
            e.printStackTrace();
            resp.sendRedirect("error.jsp");
        }
    }

    //处理更新用户请求  上面预更新已经将用户资料放进的updateUser.jsp表单中
    //所以直接用上面的createUser方法获得用户  但这方法没有userid 所以要在下面补
    private void modifyuser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String userid = req.getParameter("userid");
        Users users = this.createUser(req);
        //放进user中
        users.setUserid(Integer.parseInt(userid));

        try{
            //实例化业务务层 持久层的JDBC就会执行sql语句
            UserManagerService userManagerService = new UserManagerServiceImpl();
            userManagerService.modifyUser(users);
            //DML操作 用重定向
            resp.sendRedirect("ok.jsp");
        }catch (Exception e){
            e.printStackTrace();
            resp.sendRedirect("error.jsp");
        }
    }

    //删除用户  取到用户id 创建业务层对象 完成操作
    private void dropUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String userid = req.getParameter("userid");
        try{
            UserManagerService userManagerService = new UserManagerServiceImpl();
            userManagerService.dropUser(Integer.parseInt(userid));
            //DML操作，用重定向
            resp.sendRedirect("ok.jsp");
        }catch (Exception e){
            e.printStackTrace();
            resp.sendRedirect("error.jsp");
        }
    }
}
